<%-- 
    Document   : profile
    Created on : 21 Apr 12, 22:59:13
    Author     : hartono
--%>

<%@page import="web.vungsi"%>
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
    <h1>ini Halaman Profile lho</h1>
    <div id="bukanProfile">
      <%
      Cookie[] list = request.getCookies();
          Cookie id = null;
          if (list != null) {
            for (int i = 0; i < list.length; i++) {
              if ("id".equals(list[i].getName())) {
                id = list[i];
              }
            }
          }
          out.println(id.getValue());
        int id_login = Integer.parseInt(id.getValue());
        int author_id = Integer.parseInt(request.getParameter("pid"));
        //int author_id = 1;
        //int id_login = 18;
        String sementara = "";

        //konek ke sql
        String koneksiURL = "jdbc:mysql://localhost/progin_171_13509024?user=progin&password=progin";
        Connection koneksi = null;
        Statement statement = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        koneksi = DriverManager.getConnection(koneksiURL);
        statement = koneksi.createStatement();

        //ambil tabel User
        ResultSet ambilTebelUser = statement.executeQuery("SELECT * FROM tbl_user WHERE id=" + author_id);
        ambilTebelUser.next();
        String usernem = ambilTebelUser.getString("username");
        String email = ambilTebelUser.getString("email");
        String gender = ambilTebelUser.getString("gender");
        String Gender = "";
        if (gender == "0") {
          Gender = "laki-laki";
        } else {
          Gender = "perempuan";
        }
        String about_me = ambilTebelUser.getString("about_me");
        int post_count = ambilTebelUser.getInt("post_count");

        //ambil tabel achievement
        ResultSet ambilTebelAchievement = statement.executeQuery("SELECT * FROM tbl_achievement WHERE id in(SELECT achievement_id FROM tbl_achiever WHERE user_id=" + author_id + ")");
        String nama_achievement = "";
        String ket_achievement = "";
        if (ambilTebelAchievement.next()) {
          nama_achievement = ambilTebelAchievement.getString("name");
          ket_achievement = ambilTebelAchievement.getString("keterangan");
        }

        //untuk hitung jumlah komeng
        ResultSet ambilPostComment = statement.executeQuery("SELECT COUNT(create_time) FROM tbl_comment WHERE post_id in(SELECT id FROM tbl_post WHERE author_id=" + author_id + ")");
        int jumlahKomeng = 0;
        if (ambilPostComment.next()) {
          jumlahKomeng = (int) ambilPostComment.getInt("COUNT(create_time)");
        }

        //untuk daftar konten
        ResultSet ambilTebelKomen = statement.executeQuery("SELECT * FROM tbl_post WHERE author_id=" + author_id + " ORDER BY create_time DESC");
        String content = "";

        out.write("<strong>Username</strong><br>" + usernem + "<br><br>");
        out.write("<strong>Avatar</strong><br> <img src=upload/avatar/" + usernem + " width='20px' height='20px'/></br>");
        if (id_login == author_id) {
          out.write("<input type='file' name='avatar' accept='image/JPEG'/> <br><br>");
        }
        out.write("<strong>Email</strong><br>" + email + "<br>");
        if (id_login == author_id) {
          out.write("<form enctype='multipart/form-data'><input type='text' name='email'/> <br> <input type='submit' value='Ganti'/><br><br>");
        }
        out.write("<strong>Gender</strong><br>" + Gender + "<br><br>");
        out.write("<strong>About Me</strong><br>" + about_me + "<br><br>");
        if (id_login == author_id) {
          out.write("<textarea rows='10' cols='100' id='teksarea'></textarea> <br> <input type='button' value='Ganti' /> <br><br>");
        }
        out.write("<strong>Daftar Achievement</strong><br>");
        out.write("<strong>Nama Achievement</strong><br>" + nama_achievement + "<br>");
        out.write("<strong>Keterangan Achievement</strong><br>" + ket_achievement + "<br><br>");
        out.write("<strong>Jumlah Komentar</strong><br>" + jumlahKomeng + "<br><br>");
        out.write("<strong>Jumlah Konten yang Diupload<br></strong>" + post_count + "<br><br>");
        out.write("<strong>Daftar Konten</strong><br>");
        while (ambilTebelKomen.next() == true) {
          sementara = ambilTebelKomen.getString("title");
          out.write("<strong>Judul : </strong><a href=content.jsp class=header>" + sementara + "</a><br>");
          content = ambilTebelKomen.getString("content");
          sementara = vungsi.ambilKonten(content);
          out.write(sementara + "<br><br>");
        }
      %>
    </div>
  </body>
</html>
